package com.huawei.rbac.web.controller;

import com.huawei.rbac.domain.Role;
import com.huawei.rbac.query.QueryObject;
import com.huawei.rbac.result.PageResult;
import com.huawei.rbac.service.IPermissionService;
import com.huawei.rbac.service.IRoleService;
import com.huawei.rbac.util.RequiredRermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService service;

    @Autowired
    private IPermissionService permissionService;

    @RequestMapping("/list")
    @RequiredRermission({"角色列表","role:list"})
    public String list(Model model, QueryObject qo){
        PageResult<Role> result = service.query(qo);
        model.addAttribute("result",result);
        return "role/list";
    }

    @RequestMapping("/delete")
    @RequiredRermission({"角色删除","role:delete"})
    public String delete(Long id){
        if(id != null){
            service.delete(id);
        }
        return "redirect:/role/list";
    }

    @RequestMapping("/saveOrUpdate")
    @RequiredRermission({"角色保存或更新","role:saveOrUpdate"})
    public String saveOrUpdate(Role e,Long[] permissionIds){
        service.saveOrUpdate(e, permissionIds);
        return "redirect:/role/list";
    }

    @RequestMapping("/input")
    @RequiredRermission({"角色编辑","role:input"})
    public String input(Model model,Long id){
        if(id != null){
            Role r = service.get(id);
            model.addAttribute("r",r);
        }
        //查询高级查询表单中需要的部门信息
        model.addAttribute("permissions", permissionService.listAll());
        return "role/input";
    }
}
